package com.www.applyword.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.www.applyword.R;


/**
 * @author Administrator
 */
public class CustomLoadingDialog extends Dialog {
    private Context context;
    public static CustomLoadingDialog customProgressDialog;

    public CustomLoadingDialog(Context context) {
        super(context);
        this.context = context;
    }

    public CustomLoadingDialog(Context context, int theme) {
        super(context, theme);
    }

    public static CustomLoadingDialog createDialog(Context context) {
        customProgressDialog = new CustomLoadingDialog(context, R.style.CustomProgressDialog);
        customProgressDialog.setContentView(R.layout.cusdom_dialog);
       /// customProgressDialog.setCanceledOnTouchOutside(false);
        //customProgressDialog.show();
        //dialog弹出后会点击屏幕或物理返回键，dialog不消失
       // customProgressDialog.setCancelable(false);
        Window window = customProgressDialog.getWindow();
      //  window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        window.getAttributes().gravity = Gravity.CENTER;
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = context.getResources().getDisplayMetrics().widthPixels/5*4;
        window.setAttributes(lp);
        window.setDimAmount(0f);
        return customProgressDialog;
    }

    public static void show(Context context) {
        show(context, null, false);
    }


    private static void show(Context context, String message, boolean isCancel) {
        if (context instanceof Activity) {
            if (((Activity) context).isFinishing()) {
                return;
            }
        }
       if (customProgressDialog != null && customProgressDialog.isShowing()) {
           customProgressDialog.show();
        }else {
           customProgressDialog = CustomLoadingDialog.createDialog(context);
         //  customProgressDialog.getWindow().SetType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
           customProgressDialog.show();
       }
    }

    /**
     * dismiss the mDialogTextView
     */
    public static void dismiss(Context context) {
        try {
            if (context instanceof Activity) {
                if (((Activity) context).isFinishing()) {
                    customProgressDialog = null;
                    return;
                }
            }

            if (customProgressDialog != null && customProgressDialog.isShowing()) {
                Context loadContext = customProgressDialog.getContext();
                if (loadContext != null && loadContext instanceof Activity) {
                    if (((Activity) loadContext).isFinishing()) {
                        customProgressDialog = null;
                        return;
                    }
                }
                customProgressDialog.dismiss();
                customProgressDialog = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            customProgressDialog = null;
        }
    }



    /**
     * 和activity的onDestroy()方法同步
     */
    public void onDestroy() {
     if (customProgressDialog != null){
         customProgressDialog = null;
     }
    }
}